package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"hello/models"
	"hello/utils"
	"time"
)

var db = utils.DB

type BookController struct {
	beego.Controller
}

func (c *BookController) GetBookList() {
	page, _ := c.GetInt("page")
	size, _ := c.GetInt("size")
	if page <= 0 {
		page = 1

	}
	if size <= 0 {
		size = 5

	}
	//计算偏移
	offset := (page - 1) * size

	var books []models.Book
	count := 0
	db.Offset(offset).Limit(size).Order("update_time desc").Find(&books)
	for i := range books {
		bookType := models.BookType{}
		db.Where("type_id = ?", books[i].TypeId).First(&bookType)
		publishing := models.Publishing{}
		db.Where("publishing_id = ?", books[i].PublishingId).First(&publishing)
		books[i].BookType = bookType
		books[i].Publishing = publishing
	}
	db.Table("book").Count(&count)
	c.Data["json"] = models.PageSuccess(models.Page{
		Count: count,
		Page:  page,
		Size:  size,
		Data:  books,
	})
	c.ServeJSON()
}

func (c *BookController) GetBook() {
	bookId, err := c.GetInt("id")

	if err != nil {
		fmt.Println(err)
		return
	}

	book := models.Book{}
	db.Where("id = ?", bookId).First(&book)
	if book != (models.Book{}) {
		c.Data["json"] = models.Success(book)
		c.ServeJSON()
	}
	c.Data["json"] = models.Error(nil)
	c.ServeJSON()
}

func (c *BookController) GetBookTypeList() {
	var types []models.BookType
	db.Find(&types)
	if len(types) != 0 {
		c.Data["json"] = models.Success(types)
		c.ServeJSON()
	}
}

func (c *BookController) GetPublishingList() {
	var publishingList []models.Publishing
	db.Find(&publishingList)
	if len(publishingList) != 0 {
		c.Data["json"] = models.Success(publishingList)
		c.ServeJSON()
	}
}

func (c *BookController) UpdateBook() {
	//body := c.Ctx.Input.RequestBody
	//book := models.Book{}
	//err := json.Unmarshal(body, &book)
	//
	//if err != nil || book == (models.Book{}) {
	//	fmt.Println(err)
	//	return
	//}
	id, _ := c.GetInt("id")
	bookName := c.GetString("bookName")
	author := c.GetString("author")
	price, _ := c.GetFloat("price")
	typeId, _ := c.GetInt("typeId")
	status, _ := c.GetInt("status")
	publishingId, _ := c.GetInt("publishingId")

	book := models.Book{}
	m := map[string]interface{}{
		"id":            id,
		"book_name":     bookName,
		"author":        author,
		"Price":         float32(price),
		"typeId":        typeId,
		"publishing_id": publishingId,
		"status":        status,
		"update_time":   time.Now(),
	}
	db.Model(&book).Updates(m)
	c.Data["json"] = models.Success(nil)
	c.ServeJSON()
}

func (c *BookController) SaveBook() {
	//body := c.Ctx.Input.RequestBody
	//book := models.Book{}
	//
	//err := json.Unmarshal(body, &book)
	//if err != nil || book == (models.Book{}) {
	//	fmt.Println(err)
	//	return
	//}

	//id, _ := c.GetInt("id")
	bookName := c.GetString("bookName")
	author := c.GetString("author")
	price, _ := c.GetFloat("price")
	typeId, _ := c.GetInt("typeId")
	status, _ := c.GetInt("status")
	publishingId, _ := c.GetInt("publishingId")

	book := models.Book{
		//Id:           id,
		BookName:     bookName,
		Author:       author,
		Price:        float32(price),
		TypeId:       typeId,
		Status:       status,
		PublishingId: publishingId,
		UpdateTime:   time.Now(),
		CreateTime:   time.Now(),
	}
	db.Create(&book)
	c.Data["json"] = models.Success(nil)
	c.ServeJSON()
}

func (c *BookController) DeleteBook() {
	bookId, _ := c.GetInt("id")
	book := models.Book{Id: bookId}
	db.Delete(&book)
	c.Data["json"] = models.Success(nil)
	c.ServeJSON()
}
